package com.scatteredServer.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scatteredServer.common.Result;
import com.scatteredServer.pojo.entity.FileType;
import com.scatteredServer.pojo.form.add.AddFileType;
import com.scatteredServer.pojo.form.update.UpdateFileType;
import com.scatteredServer.service.impl.FileTypeServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("s4/fileType")
@Slf4j
@Tag(name = "文件类型管理", description = "文件类型管理")
public class FileTypeController {


    @Autowired
    FileTypeServiceImpl fileTypeService;

    @Operation(summary = "查询获取", description = "查询获取")
    @GetMapping("/getList")
    public Result<List<FileType>> getList() {
        List<FileType> list = fileTypeService.list();
        return Result.success(list);
    }

    @Transactional
    @Operation(summary = "批量删除", description = "批量删除")
    @DeleteMapping("/deleteByIds")
    public Result<String> deleteById(@RequestBody List<Integer> list) {
        fileTypeService.removeBatchByIds(list);
        return Result.success("删除成功");
    }

    @Operation(summary = "添加", description = "添加")
    @PostMapping("/addOne")
    public Result<String> addOne(@RequestBody AddFileType addFileType) {
        FileType fileType = new FileType();
        BeanUtil.copyProperties(addFileType,fileType);
        QueryWrapper<FileType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("file_type_name",addFileType.getFileTypeName());
        long count = fileTypeService.count(queryWrapper);
        if(count>0){
            return Result.fail("添加失败，已存在该文件类型");
        }
        fileTypeService.save(fileType);
        return Result.success("添加成功");
    }

    @Operation(summary = "修改", description = "修改")
    @PostMapping("/update")
    public Result<String> updateById(@RequestBody UpdateFileType updateFileType) {
        fileTypeService.updateById(updateFileType);
        return Result.success("修改成功");
    }

}
